#include "expense_struct_csv.hpp"
#include "type_conversion.hpp"
#include "datetime_period.hpp"
#include "allowance_type_detail.hpp"
#include "activity.hpp"


expense_struct_csv::expense_struct_csv(std::vector<std::string> const& v)
{
		
	   switch(v.size()-1)
 	   {
		case DESCR: 
			set_descr(v[DESCR]);
		case ACTIVITY_ID: 
			set_activity_id(type_conversion<int>::to_number(v[ACTIVITY_ID]));
		case CHILD_ALLOWANCE_ID:
			set_child_allowance_id(type_conversion<int>::to_number(v[CHILD_ALLOWANCE_ID]));
		case ALLOWANCE_ID:
			set_allowance_id(type_conversion<int>::to_number(v[ALLOWANCE_ID]));
		case AMOUNT:
			set_amount(type_conversion<float>::to_number(v[AMOUNT]));
		case DAY:
			set_day(date_input_string::to_date(v[DAY]));
		case ID:
			set_id(type_conversion<int>::to_number(v[ID]));
			break;
		default: break;
	   }
		
		std::cout << "[expense_struct_csv.expense_struct_csv] " <<*this << std::endl;
}



int expense_struct_csv::find_daily_expenses(float list[])
{
	int allowance_id(this->allowance_id());

	//we should check for unlimited allowances
	if(static_cast<activity_struct_csv>(activity_csv::find(this->activity_id())).limiting_allowance())
	{
		float amount(allowance_type_detail_csv::find_limit(allowance_id, this->child_allowance_id()));

		// find any maximum limit on total allowance
		amount = amount? std::min(amount, this->amount()) : this->amount();

		list[0] += amount;
		list[1] += this->amount() - amount;
	}
	else
	{
		list[0] += this->amount();
	}	

	return allowance_id;
	
}
